﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Thread
{
    /// <summary>
    /// 武将升级线程
    /// </summary>
    class OfficerUpgradeThread : BaseSangoThread
    {
        public override void Run(object state, bool timeOut)
        {
            if (IsDisposed)
                return;

            if (!IsRunning)
                return;

            LoggerHelper.Debug("begin officer upgrade thread.");

            foreach (Entities.Usr_Officer officer in OfficerDao.getTrainningOfficers())
            {
                Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(officer.Usercode);

                Entities.Usr_Building officerBuilding = UsrBuildingDao.getUserBuilding(officer.Usercode, CtiBuildingTypecodeCollection.TRAIN);

                IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

                command.Begin(delegate()
                {
                    long currentdate = GlobalTimer.Instance.ToTimeSequence();
                    long enddate = (long)officer.Upgradeenddate < currentdate ? (long)officer.Upgradeenddate : currentdate;

                    // 修改经验值

                    double increaseExp = OfficerFormular.getUpgradingExperienceByBuilding(//
                            officerBuilding.Buildinglevel, //
                            officer.Upgradestartdate, //
                            enddate, //
                            officer.Upgradespeedtypecode);

                    OfficerFormular.upgrade(officer, increaseExp, profile.Userlevel, officerBuilding.Buildinglevel);

                    OfficerDao.stopUpgrade(officer);

                    command.Update(officer);

                    command.Commit();
                });
            }
        }

        public static long interval
        {
            get
            {
                return 1000 * 10;
            }
        }
    }
}
